#!M:/WinPython-64bit-3.4.4.3Qt5/python-3.4.4.amd64/python.exe
#coding=utf-8
print("Content-Type:text/html\n\n")

print("<!DOCTYPE html>")
print("<html lang='zh-Hans'>")
print("<head><meta http-equiv='Content-Type' content='text/html';charset='utf-8'>")
print("<script src='//cdn.bootcss.com/jquery/3.1.0/jquery.js'></script>")
print("</head>")
print("<body>")
print("<form enctype='multipart/form-data' method='post' action='javascript:ajax_submit();'>")
print("<input id='file' type='file'/><br/><br/>")
print("<input type='submit'/>")
print("</form>")
print("""<div id='propressBar'>
              <span id='filesize'>
              </span>
              <p><b id='rate'></b></p>


      """)
print("<p id='p_'></p>")
print("""<script>
	ajax_submit=function(){
		var img_=document.forms[0]['file'].files[0]  
		//alert(img_)
		reader=new FileReader()
		reader.readAsDataURL(img_)
          reader.onloadstart=function(){
              //alert('start!')
              if(img_.size>0 && img_.size<1024*1024)
              {
                var k_size=(img_.size/1024.0).toFixed(2)+'K'
              }
              else
              {
                var m_size=(img_.size/(1024.0*1024.0)).toFixed(2)+'M'
              }
              //alert(m_size || k_size )
              size=k_size || m_size
              document.getElementById('filesize').textContent='文件大小: '+size
          }
          reader.onprogress=function(event){
              var value=((event.loaded/img_.size).toFixed(2)*100)
              var text='读取进度: '+value+'%'
              document.getElementById('rate').textContent=text
          }
		reader.onload=function(){
			$.ajax({
				url:'/cgi-bin/handle_upload.py',
				type:'POST',
				data:{'filename':img_.name,'file':reader.result},
				success:function(data,status,xhr){
				   document.getElementById('p_').innerHTML=data
				}
			})
		}
		
	}       
</script>""")
print("<body>")
print("<html>")